package erwiki.ui.app.admin.views;

import java.util.List;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.elwiki_data.PageReference;
import org.elwiki_data.WikiPage;

import erwiki.api.core.Engine;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class DataInspecting {

	public static final String ID = "org.elwiki.e4.ui.app.admin.part.datainspecting"; //$NON-NLS-1$

	private Text txtUsedReferences;
	private Text txtUnusedReferences;
	private Text txtOfMain;

	@Inject
	Engine engine;

	// == CODE ================================================================
	
	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 * @wbp.parser.entryPoint
	 */
	@PostConstruct
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(1, false));
		{
			Group grpPagereferences = new Group(container, SWT.NONE);
			grpPagereferences.setText("PageReferences");
			grpPagereferences.setLayout(new GridLayout(2, false));

			Label lblUsedReferences = new Label(grpPagereferences, SWT.NONE);
			lblUsedReferences.setBounds(0, 0, 71, 17);
			lblUsedReferences.setText("Used references:");

			this.txtUsedReferences = new Text(grpPagereferences, SWT.BORDER | SWT.READ_ONLY);
			this.txtUsedReferences.setBounds(0, 0, 81, 29);

			Label lblUnusedReferences = new Label(grpPagereferences, SWT.NONE);
			lblUnusedReferences.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblUnusedReferences.setBounds(0, 0, 71, 17);
			lblUnusedReferences.setText("Unused references:");

			this.txtUnusedReferences = new Text(grpPagereferences, SWT.BORDER | SWT.READ_ONLY);
			this.txtUnusedReferences.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

			Label lblOfmain = new Label(grpPagereferences, SWT.NONE);
			lblOfmain.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
			lblOfmain.setText("of 'Песочница':");

			this.txtOfMain = new Text(grpPagereferences, SWT.BORDER | SWT.READ_ONLY);
			this.txtOfMain.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

			Button btnRefresh = new Button(grpPagereferences, SWT.NONE);
			btnRefresh.addMouseListener(new MouseAdapter() {
				private static final long serialVersionUID = 6146251553052045917L;

				@Override
				public void mouseUp(MouseEvent e) {
					updateReferences();
				}
			});
			btnRefresh.setText("Refresh");
			new Label(grpPagereferences, SWT.NONE);
		}

		/*:FVK: -- old code
		createActions();
		initializeToolBar();
		initializeMenu();*/
	}

	protected void updateReferences() {
		/*TODO: сделать для новой E4.
		
		IApplicationSession applicationSession = AdminAppUiActivator.getDefault().getAppSession();
		IReferenceManager referenceManager = applicationSession.getReferenceManager();

		List<PageReference> pageReferences = referenceManager.getAllPageReferences();
		int used = 0;
		int unused = 0;
		int ofMainPage = 0;
		for (PageReference pageReference : pageReferences) {
			WikiPage wp = pageReference.getWikipage();
			if (wp != null) {
				used++;
				if (wp.getName().equals("Песочница")) {
					ofMainPage++;
				}
			} else {
				unused++;
			}
		}
		this.txtUsedReferences.setText(String.valueOf(used));
		this.txtUnusedReferences.setText(String.valueOf(unused));
		this.txtOfMain.setText(String.valueOf(ofMainPage));

		*/
	}

}
